import { fileURLToPath, URL } from 'node:url';
import path from 'path';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import vueDevTools from 'vite-plugin-vue-devtools';
import { terser } from 'rollup-plugin-terser';
import AutoImport from 'unplugin-auto-import/vite';

const __filenameNew = fileURLToPath(import.meta.url);
const __dirnameNew = path.dirname(__filenameNew);

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
    AutoImport({
      // 导入Vue相关的API
      imports: ['vue', 'vue-router']
    })
  ],
  build: {
    lib: {
      entry: path.resolve(__dirnameNew, 'src/supreme/index.js'),
      name: 'supreme' // For IIFE.
    },
    rollupOptions: {
      // Make sure to externalize deps that shouldn't be bundled into library.
      external: ['vue', 'vue-router', '@nutui/nutui', '@tarojs/taro', 'lodash-es', 'axios'],
      output: [
        // Works with dynamic import().
        {
          format: 'es',
          dir: 'dist',
          entryFileNames: 'index.js',
          chunkFileNames: '[name].js',
          plugins: [terser()] // Minify.
        }
      ]
    }
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@import "src/assets/styles/common.scss";`
      }
    }
  }
});
